<?php
/**
 * Favorites page controller
 *
 * @author Dima
 *
 */

class FavoritesController extends modules_default_controllers_ControllerBase
{
	protected $_bForLoggedUsersOnly = true;
	public $ajaxable = array(
		'add' => array('json'),
		'remove' => array('json'),
		'update' => array('json'),
	);

//----------------------------------------------------------------------------------------------------

	public function init()
	{
		if (! $this->getRequest()->isXmlHttpRequest())
		{
			$this->view->title = 'Favorites';
			parent::init();
		}

		$this->_helper->AjaxContext()->initContext('json');
	}

//----------------------------------------------------------------------------------------------------

	public function indexAction()
	{

	}

//----------------------------------------------------------------------------------------------------

	public function addAction()
	{
		$oRequest 		= $this->getRequest();
		$favCategory	= $oRequest->getParam('fav_category');		
		$newFavCategory	= $oRequest->getParam('new_fav_category');
		$idFavItem		= $oRequest->getParam('id_fav_item');
		$idUser			= Lemar_User::getLoggedUserId();
		$ret 			= 0;
		$fav_id			= null;

		if($newFavCategory)
		{
			$favCategory = models_FavoritecategoryMapper::save(array('name' => $newFavCategory));
		}
		
		try 
		{
			if( $fav_id = models_FavoriteMapper::save(array(
												 'card_id' 				=> $idFavItem,
												 'favorite_category_id' => $favCategory,
												 'user_id'				=> $idUser
											)) )
			$ret = 1;
		} 
		catch (Exception $e) 
		{
			$ret = 0;
		}
		
		$this->view->ret 	= $ret;
		$this->view->fav_id = $fav_id;
	}

//----------------------------------------------------------------------------------------------------

	public function updateAction()
	{
	}

//----------------------------------------------------------------------------------------------------

	public function removeAction()
	{
		$idItem = (int)$this->_getParam('id_fav_item',null);
		$idUser	= Lemar_User::getLoggedUserId();

		$this->view->ret = models_FavoriteMapper::delete($idItem, $idUser);
	}

//----------------------------------------------------------------------------------------------------

}
